<template>
    <div class="app-container">
        <el-form :model="menu" :rules="rules" ref="ruleForm" label-width="200px" class="demo-ruleForm">
            <el-form-item label="显示名" prop="name" >
                <el-input v-model="menu.nam"></el-input>
            </el-form-item>
            <el-form-item label="链接URL" prop="url" >
                <el-input v-model="menu.url"></el-input>
            </el-form-item>
            <el-form-item label="备注" prop="remarks" >
                <el-input v-model="menu.remarks"></el-input>
            </el-form-item>
            <el-form-item label="显示排序" prop="orderNum" >
                <el-input-number v-model="menu.orderNum" :min="1" :max="999" label="排序号"></el-input-number>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
                <el-button @click="resetForm('ruleForm')">重置</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
  import { addMenuApi } from '@/api/menu'
  export default {
    data() {
      return {
        menu: {
          type: 1,
          orderNum: 1
        },
        rules: {
          menuUrl: [
            { required: true, message: '请输入链接URL', trigger: 'blur' }
          ],
          orderNum: [
            { required: true, message: '请输入排序号', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            var form = new FormData();
            Object.keys(this.menu).forEach((key)=>{
              form.append(key,this.menu[key]);
            })
            addMenuApi(form)
              .then(response=>{
                if(response.status == 0){
                  this.$message("提交成功")
                }
                this.$router.push("/content/topmenu")
              })
          } 
        })
      },
      resetForm(formName) {
        this.$refs[formName].resetFields()
      }
    }
  }
</script>